/*
* Powered By [choudou5]
* Web Site: http://www.hsolr.com
* License：MIT
* Since 2018 - 2020
*/
package com.choudou5.solr.service.sys;

import cn.hutool.core.collection.CollUtil;
import com.choudou5.solr.annotation.ServiceInfo;
import com.choudou5.solr.bean.sys.SysDictBean;
import com.choudou5.solr.framework.constants.CacheConsts;
import com.choudou5.solr.framework.constants.SysConsts;
import com.choudou5.solr.framework.util.UserUtil;
import com.choudou5.solr.service.BaseService;
import com.choudou5.solr.util.DateUtil;
import com.choudou5.solr.util.exception.BizException;
import com.choudou5.solr.util.jdbc.SqlFormatUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @name 字典类型表 Service
 * @author choudou5
 * @date：2018-08-06
 */
@ServiceInfo(table = "solr_sys_dict", desc = "字典类型表")
public class SysDictService extends BaseService<SysDictBean> {


    @Override
    public void save(SysDictBean bean) throws BizException {
        super.save(bean);
        removeCache(bean.getDictType());
    }


    public SysDictBean getByDictType(String dictType) {
        SysDictBean bean = findOne("dict_type = '" + dictType + "'");
        return bean;
    }


    public void insertDictVal(String title, String dictType, String dictVal) {
        SysDictBean bean = getByDictType(dictType);
        if(bean == null){
            bean = new SysDictBean(dictType, title, dictVal);
            add(bean);
        }
    }

    public void updateDictVal(String title, String dictType, String dictVal) {
        SysDictBean bean = getByDictType(dictType);
        if(bean == null){
            bean = new SysDictBean(dictType, title, dictVal);
            add(bean);
        }else{
            updateValueByType(bean.getId(), dictVal);
        }
    }


    public void updateValueByType(Integer id, String dicValue) {
        String sql = "UPDATE "+getTable()+" SET dict_value = ?, update_by = ?, update_time = ? WHERE id = ? ";
        boolean flag = update(SqlFormatUtil.format(sql, dicValue, UserUtil.getUserId(), DateUtil.now(), id));
        if(flag)
            removeCache(get(id).getDictType());
        addSynDynamic(SysConsts.ActionType.UPDATE, "更新字典", "更新值为:"+dicValue);
    }

    public List<String> getListValueByType(String dictType){
        List<SysDictBean> dictList = getListByType(dictType);
        if(CollUtil.isEmpty(dictList))
            return null;
        List<String> values = new ArrayList<>();
        for (SysDictBean dict : dictList) {
            values.add(dict.getDictValue());
        }
        return values;
    }

    public List<SysDictBean> getListByType(String dictType){
        String cacheKey = CacheConsts.SYS_DICT + dictType;
        List<SysDictBean> dictList = cacheHelper.getList(cacheKey, SysDictBean.class);
        if(CollUtil.isEmpty(dictList)){
            String where = "status = 1 AND dict_type = ?";
            dictList = findList(SqlFormatUtil.format(where, dictType));
            if(CollUtil.isEmpty(dictList))
                return null;
            cacheHelper.put(cacheKey, dictList);
        }
        return dictList;
    }

    private void removeCache(String dictType) {
        String cacheKey = CacheConsts.SYS_DICT + dictType;
        cacheHelper.remove(cacheKey);
    }
}